import 'package:flutter/material.dart';

class Country {
  Image country;
  int golden;
  int silver;
  int copper;

  Country(this.country, this.golden, this.silver, this.copper);
}

class Olympic09 extends StatefulWidget {
  Olympic09({Key? key}) : super(key: key);

  @override
  Olympic09State createState() => Olympic09State();
}

class Olympic09State extends State<Olympic09> {
  List<Country> data = [
    Country(
        Image.asset("assets/images/olympic/09/America.png",
            width: 50, height: 34, fit: BoxFit.fill),
        39,
        41,
        33),
    Country(
        Image.asset("assets/images/olympic/09/China.png",
            width: 50, height: 34, fit: BoxFit.fill),
        38,
        32,
        18),
    Country(
      Image.asset("assets/images/olympic/09/Japan.png", width: 50, height: 34, fit: BoxFit.fill),
      27,
      14,
      17,
    ),
  ];

  var _sortAscending = true;
  var _sortColumnIndex = 0;

  List<DataRow> getCountryRows() {
    List<DataRow> dataRows = [];
    for (int i = 0; i < data.length; i++) {
      dataRows.add(DataRow(cells: [
        DataCell(data[i].country),
        DataCell(Text('${data[i].golden}')),
        DataCell(Text('${data[i].silver}')),
        DataCell(Text('${data[i].copper}')),
      ]));
    }

    return dataRows;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: DataTable(
            sortColumnIndex: _sortColumnIndex,
            sortAscending: _sortAscending,
            dataRowHeight: 60,
            horizontalMargin: 20,
            columnSpacing: 27,
            columns: [
              DataColumn(label: Text('国家')),
              DataColumn(
                  label: Text('金'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort((a, b) => a.golden.compareTo(b.golden));
                      } else {
                        data.sort((a, b) => b.golden.compareTo(a.golden));
                      }
                    });
                  }),
              DataColumn(
                  label: Text('银'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort((a, b) => a.silver.compareTo(b.silver));
                      } else {
                        data.sort((a, b) => b.silver.compareTo(a.silver));
                      }
                    });
                  }),
              DataColumn(
                  label: Text('铜'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort((a, b) => a.copper.compareTo(b.copper));
                      } else {
                        data.sort((a, b) => b.copper.compareTo(a.copper));
                      }
                    });
                  }),
            ],
            rows: getCountryRows(),
          )),
    );
  }
}
